home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSFILE.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  5KB  |  184 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/dosfile.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "msdos.h"
  36. #include "osfile.h"
  37. #include "dosio.h"
  38.  
  39. extern void EXFUN (terminal_open, (Tchannel channel));
  40.  
  41. static enum channel_type
  42. DEFUN (fd_channel_type, (fd), int fd)
  43. {
  44.   struct stat stat_buf;
  45.   if ((DOS_fstat (fd, (&stat_buf))) < 0)
  46.     return (channel_type_unknown);
  47.   {
  48.     mode_t type = ((stat_buf . st_mode) & S_IFMT);
  49.     return
  50.       ((type == S_IFREG) ? channel_type_file
  51.        : (type == S_IFCHR)
  52.        ? ((isatty (fd))
  53.       ? channel_type_terminal
  54.       : channel_type_character_device)
  55. #ifdef S_IFIFO
  56.        : (type == S_IFIFO) ? channel_type_fifo
  57. #endif
  58. #ifdef S_IFBLK
  59.        : (type == S_IFBLK) ? channel_type_block_device
  60. #endif
  61.        : (type == S_IFDIR) ? channel_type_directory
  62.        : channel_type_unknown);
  63.   }
  64. }
  65.  
  66. Tchannel
  67. DEFUN (OS_open_fd, (fd), int fd)
  68. {
  69.   enum channel_type type = (fd_channel_type (fd));
  70.   Tchannel channel;
  71.   MAKE_CHANNEL (fd, type, channel =);
  72.  
  73.   /* Like Unix, all terminals initialize to cooked mode. */
  74.   if (type == channel_type_terminal) CHANNEL_COOKED(channel) = 1;
  75.  
  76.   return (channel);
  77. }
  78.  
  79. static Tchannel
  80. DEFUN (open_file, (filename, oflag), CONST char * filename AND int oflag)
  81. {
  82.   int fd;
  83.   STD_UINT_SYSTEM_CALL
  84.     (syscall_open, fd, (DOS_open (filename, oflag, MODE_REG)));
  85.   return (OS_open_fd (fd));
  86. }
  87.  
  88. #define DEFUN_OPEN_FILE(name, oflag)                    \
  89. Tchannel                                \
  90. DEFUN (name, (filename), CONST char * filename)                \
  91. {                                    \
  92.   return (open_file (filename, oflag));                    \
  93. }
  94.  
  95. DEFUN_OPEN_FILE (OS_open_input_file, O_RDONLY)
  96. DEFUN_OPEN_FILE (OS_open_output_file, (O_WRONLY | O_CREAT | O_TRUNC))
  97. DEFUN_OPEN_FILE (OS_open_io_file, (O_RDWR | O_CREAT))
  98.  
  99. #ifdef HAVE_APPEND
  100.  
  101. DEFUN_OPEN_FILE (OS_open_append_file, (O_WRONLY | O_CREAT | O_APPEND))
  102.  
  103. #else
  104.  
  105. Tchannel
  106. DEFUN (OS_open_append_file, (filename), CONST char * filename)
  107. {
  108.   error_unimplemented_primitive ();
  109.   return (0);
  110. }
  111.  
  112. #endif
  113.  
  114. static Tchannel
  115. DEFUN (make_load_channel, (fd), int fd)
  116. {
  117.   enum channel_type type = (fd_channel_type (fd));
  118.   if ((type == channel_type_terminal)
  119.       || (type == channel_type_directory)
  120.       || (type == channel_type_unknown))
  121.     return (NO_CHANNEL);
  122.   MAKE_CHANNEL (fd, type, return);
  123. }
  124.  
  125. Tchannel
  126. DEFUN (OS_open_load_file, (filename), CONST char * filename)
  127. {
  128.   while (1)
  129.     {
  130.       int fd = (DOS_open (filename, O_RDONLY, MODE_REG));
  131.       if (fd >= 0)
  132.     return (make_load_channel (fd));
  133.       if (errno != EINTR)
  134.     return (NO_CHANNEL);
  135.     }
  136. }
  137.  
  138. Tchannel
  139. DEFUN (OS_open_dump_file, (filename), CONST char * filename)
  140. {
  141.   while (1)
  142.     {
  143.       int fd = (DOS_open (filename, (O_WRONLY | O_CREAT | O_TRUNC), MODE_REG));
  144.       if (fd >= 0)
  145.     return (make_load_channel (fd));
  146.       if (errno != EINTR)
  147.     return (NO_CHANNEL);
  148.     }
  149. }
  150.  
  151. off_t
  152. DEFUN (OS_file_length, (channel), Tchannel channel)
  153. {
  154.   struct stat stat_buf;
  155.   STD_VOID_SYSTEM_CALL
  156.     (syscall_fstat, (DOS_fstat ((CHANNEL_DESCRIPTOR (channel)), (&stat_buf))));
  157.   return (stat_buf . st_size);
  158. }
  159.  
  160. off_t
  161. DEFUN (OS_file_position, (channel), Tchannel channel)
  162. {
  163.   off_t result;
  164.   STD_UINT_SYSTEM_CALL
  165.     (syscall_lseek,
  166.      result,
  167.      (DOS_lseek ((CHANNEL_DESCRIPTOR (channel)), 0L, SEEK_CUR)));
  168.   return (result);
  169. }
  170.  
  171. void
  172. DEFUN (OS_file_set_position, (channel, position),
  173.        Tchannel channel AND
  174.        off_t position)
  175. {
  176.   off_t result;
  177.   STD_UINT_SYSTEM_CALL
  178.     (syscall_lseek,
  179.      result,
  180.      (DOS_lseek ((CHANNEL_DESCRIPTOR (channel)), position, SEEK_SET)));
  181.   if (result != position)
  182.     error_external_return ();
  183. }
  184.